<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('Recommendseetype');
Globals::requireTable('Recommendsee');
Globals::requireTable('Recommendseetouid');
Globals::requireTable('Recommendseedetail');

class RecommendcollectController extends Controller
{
    protected $Recommendseetype;
    protected $Recommendsee;
    protected $Recommendseetouid;
    protected $Recommendseedetail;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    parent::__construct($config);
	    $this->Recommendseetype = new RecommendseetypeTable($config);
	    $this->Recommendsee = new RecommendseeTable($config);
	    $this->Recommendseetouid = new RecommendseetouidTable($config);
	    $this->Recommendseedetail = new RecommendseedetailTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	
	public function collectAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>''));
	        exit();
	    }
	    
	    $id = $this->getIntParam('id');
	    $iscollect = $this->getIntParam('iscollect');
	    
	    if($iscollect == 1){
	        $data = array(
	            'rid' => $id,
	            'uid' => $uid
	        );
	        $this->Recommendseetouid->add($data);
	        $message = '我有一个小秘密就不告诉你';
	    }else{
	        $data = array(
	            'rid' => $id,
	            'uid' => $uid
	        );
	        $this->Recommendseetouid->delete($data);
	        $message = '离开我希望你过的幸福';
	    }
	    
	    echo json_encode(array('returncode'=>'0','message'=>$message));
	    exit();
	}
	
	public function collectlistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $type = $this->getIntParam('type');
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 1 : $page;
	    $size = $this->getIntParam('size');
	    $size = $size < 1 ? 10 : $size;
	    
	    
	    $list = array();
	    
	    $collect_data = $this->Recommendseetouid->listPage(array('uid'=>$uid),'id desc',$size, $page);
	    foreach ($collect_data as $v){
	        $data = $this->Recommendsee->getRow($v['rid']);
	        
	        $tmp_detail = array();
	        $detail_data = $this->Recommendseedetail->listAll(array('rid'=>$data['id']));
	        foreach ($detail_data as $v2){
	            $tmpd = array(
	                'icon' => $v2['icon'],
	                'name' => $v2['name'],
	                'describe' => $v2['remark'],
	                'playtypename' => $v2['playtypename'],
	                'playurl' => $v2['playurl']
	            );
	            $tmp_detail[] = $tmpd;
	        }
	        
	        $tmp = array(
	            'title' => $data['title'],
	            'icon' => $data['icon'],
	            'describe' => $data['remark'],
	            'collect' => 1,
	            'id' => $data['id'],
	            'playterrace' => $tmp_detail
	        );
	        
	        $list[] = $tmp;
	    }
	    
	    echo json_encode(array('returncode'=>'0','list'=>$list));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('RecommendcollectController', 'Controller');
